package io.gitee.wiqer.simple;

import io.gitee.wiqer.ListNode;

public class SolutionNC25_DeleteDuplicates {
    /**
     * 删除给出链表中的重复元素（链表中元素从小到大有序），使链表中的所有元素都只出现一次
     * @param head
     * @return
     */
    public ListNode deleteDuplicates (ListNode head) {
        // write code here
        ListNode cur = head;
        ListNode tmp = null;
        while(cur!=null){
            if(tmp!=null&&cur.val==tmp.val){
                tmp.next=cur.next;
            }else{
                tmp=cur;
            }
            cur=cur.next;

        }
        return head;
    }

    /**
     * 抖机灵，思维比较跳跃，比较难记
     * @param head
     * @return
     */
    public ListNode deleteDuplicates_Jump (ListNode head) {
        if(head==null)return head;
        ListNode temp = head;
        while(temp.next!=null){
            if(temp.val==temp.next.val){
                temp.next=temp.next.next;
            }else{
                temp=temp.next;
            }
        }
        return head;
    }
}
